import {MissonModel,TerminalResource} from '../Misson/missonModel'
/* 定义Manager角色相关任务 */
export default class RoomTaskManager extends Room {
    /* 链接送仓任务监控 */
    public LToS_PAS():void{
        if (Game.time % 5) return
        if (!this.memory.TaskMemory.center_link) return
        var center_link = Game.getObjectById(this.memory.TaskMemory.center_link as string) as StructureLink
        if (!center_link) {delete this.memory.TaskMemory.center_link;return}
        if (!this.CheckInMisson('CREEP','链接送仓'))
        {
            /* 能量大于等于600就挂载任务 */
            if (center_link.store['energy'] >= 600 && this.memory.TaskMemory.storageID)
            {
                let thisTask:RoomTask = {} as RoomTask
                Object.assign(thisTask,MissonModel['链接送仓'])
                thisTask.relateCreep = ['manage01']
                this.mountMisson(thisTask)
            }
        }
    }
    /* 终端送仓任务监控  */
    public TToS_PAS():void{
        if (Game.time % 7) return
        if (this.CheckInMisson('CREEP','资源传送')) return
        if (!this.memory.TaskData) this.memory.TaskData = {}
        if (!this.memory.TaskMemory.storageID || !this.memory.TaskMemory.terminalID) return
        if (this.CheckInMisson('CREEP','终端送仓')) return
        var storage_ = Game.getObjectById(this.memory.TaskMemory.storageID as string) as StructureStorage
        var terminal_ = Game.getObjectById(this.memory.TaskMemory.terminalID as string) as StructureTerminal
        if (!storage_) {delete this.memory.TaskMemory.storageID;return}
        if (!terminal_){delete this.memory.TaskMemory.terminalID;return}
        var storeObjects = terminal_.store
        if (!this.memory.TaskData.terminalResource) this.memory.TaskData.terminalResource = TerminalResource
        for (var resource_ in storeObjects)
        {
            /* 如果不存在就进行初始化 */
            if (!this.memory.TaskData.terminalResource[resource_])
            {
                this.memory.TaskData.terminalResource[resource_]= {num:2000,fill:false}
            }
            /* 资源超过上限 + 500，就挂载任务 */
            if (storeObjects[resource_] - 500 > this.memory.TaskData.terminalResource[resource_].num && !this.CheckInMisson('CREEP','终端送仓'))
            {
                let thisTask:RoomTask = {} as RoomTask
                Object.assign(thisTask,MissonModel['终端送仓'])
                thisTask.relateCreep = ['manage01']
                thisTask.relateResource = [resource_ as ResourceConstant]
                this.mountMisson(thisTask)
                return
            }
        }

    }
    /* 近塔填充任务监控 */
    public SToNT_PAS():void{
        if (Game.time % 13) return
        if (!this.memory.Task['CREEP']) this.memory.Task['CREEP'] = []
        if (!this.memory.TaskMemory.storageID || !this.memory.TaskMemory.NtowerID) return
        var NTower = Game.getObjectById(this.memory.TaskMemory.NtowerID as string) as StructureTower
        var storage_ = Game.getObjectById(this.memory.TaskMemory.storageID as string)
        if (!NTower || !storage_)
        {
            delete this.memory.storageID
            delete this.memory.NtowerID
            return
        }
        /* 如果NTower的能量储备低于600就触发近塔填充任务 */
        if (NTower.store.getUsedCapacity(RESOURCE_ENERGY) < 600 && !this.CheckInMisson('CREEP','近塔填充'))
        {
            let thisTask:RoomTask = {} as RoomTask
            Object.assign(thisTask,MissonModel['近塔填充'])
            thisTask.relateCreep = ['manage01']
            this.mountMisson(thisTask)
        }
    }
    /* 仓送终端任务监控 */
    public SToT_PAS():void{
        if (Game.time % 23) return
        if (!this.memory.TaskData) this.memory.TaskData = {}
        if (!this.memory.TaskMemory.storageID || !this.memory.TaskMemory.terminalID) return
        if (this.CheckInMisson('CREEP','仓送终端')) return
        var storage_ = Game.getObjectById(this.memory.TaskMemory.storageID as string) as StructureStorage
        var terminal_ = Game.getObjectById(this.memory.TaskMemory.terminalID as string) as StructureTerminal
        if (!storage_) {delete this.memory.TaskMemory.storageID;return}
        if (!terminal_){delete this.memory.TaskMemory.terminalID;return}
        var storeObjects = terminal_.store
        if (!this.memory.TaskData.terminalResource) this.memory.TaskData.terminalResource = TerminalResource
        for (var resource_ in storeObjects)
        {
            /* 如果不存在就进行初始化 */
            if (!this.memory.TaskData.terminalResource[resource_])
            {
                this.memory.TaskData.terminalResource[resource_]= {num:2000,fill:false}
            }
            /* 资源低于规定量1000以上并且资源fill属性为true */
            if (this.memory.TaskData.terminalResource[resource_].fill == true && storeObjects[resource_]  <  this.memory.TaskData.terminalResource[resource_].num - 1000 && !this.CheckInMisson('CREEP','仓送终端'))
            {
                /* 仓库资源不够就不挂载该任务 */
                if (storage_.store[resource_] - 1000 < this.memory.TaskData.terminalResource[resource_] - storeObjects[resource_])
                {
                    return
                }
                let thisTask:RoomTask = {} as RoomTask
                Object.assign(thisTask,MissonModel['仓送终端'])
                thisTask.relateCreep = ['manage01']
                thisTask.relateResource = [resource_ as ResourceConstant]
                this.mountMisson(thisTask)
                return
            }
        }
    }
    /* 资源传送任务处理 */
    public TToT_AC():void{
        if (!this.memory.Task['CREEP']) this.memory.Task['CREEP'] = []
        if (!this.CheckInMisson('CREEP','资源传送')) return
        var terminal_ = Game.getObjectById(this.memory.TaskMemory.terminalID as string) as StructureTerminal
        var thisTask = this.getMisson('CREEP','资源传送')
        if (thisTask.state == 0)
        {
            /* 传送一阶段 -> 传送二阶段 判断逻辑 */
            if (terminal_.store['energy'] >= thisTask.expense)
                thisTask.state = 1
        }
        else if (thisTask.state == 1)
        {
            if (thisTask.relateResource.length <= 0) return
            if (terminal_.store['energy'] < thisTask.expense) thisTask.state = 0
            /* 传送二阶段 -> 传送末阶段 判断逻辑 */
            if (thisTask.relateResource[0] == 'energy')
            {
                /* 是能量，则判断能量总和是否高于路费和所需能量的总和 */
                if (terminal_.store[thisTask.relateResource[0]] >= thisTask.expense + thisTask.num)
                    thisTask.state = 2
            }
            else
            {
                if (terminal_.store[thisTask.relateResource[0]] >= thisTask.num)
                    thisTask.state = 2
            }
        }
        else if (thisTask.state == 2)
        {
            /* 传送末阶段 处理终端的发送逻辑 */
            var result = terminal_.send(thisTask.relateResource[0],thisTask.num,thisTask.disRoom[0])
            if (result == OK) {
                console.log(`[terminal] 房间${this}发送资源成功!`)
                this.deleteMisson_ID(thisTask.ID)
            }
            else if (result == -11){
                /* 冷却中 */
                console.log(`[terminal] 房间${this}的终端冷却中，推迟发送!`)
                return
            }
            else{
                console.log(`[terminal] 房间${this}发送资源失败!! 错误码为${result}`)
                this.deleteMisson_ID(thisTask.ID)
            }
        }
    }
}